/**
 * 
 */
package com.taobao.snowsnow.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.taobao.snowsnow.hibernate.dao.DimensionDao;
import com.taobao.snowsnow.hibernate.pojo.Dimension;

/**
 * @author Tux
 *
 * 创建时间: 2011-4-29 上午09:45:11
 */
@SuppressWarnings("unchecked")
public class DimensionDaoImpl extends BaseDaoImpl implements DimensionDao {
	
	public Dimension findById(int id) throws Exception {
		return (Dimension) getHibernateTemplate().get(Dimension.class, id);
	}
	
	public List<Dimension> findByName(String name) throws Exception {
		return super.findByProperty(Dimension.class, "name", name);
	}

	public List<Dimension> findAll() throws Exception {
		return getHibernateTemplate().find("from Dimension");
	}

	public long getTotal() throws Exception {
		return super.countByCriteria(Dimension.class);
	}

	public List<Dimension> paging(int page, int limit) throws Exception {
		return super.findByCriteriaByPage(Dimension.class, page, limit);
	}

	@Override
	public Dimension getFirstObject() throws Exception {
		Query query = getSession().createQuery("from Dimension as o where o.id = (select min(id) from Dimension)");
		List<Dimension> list = query.list();
		if(list.size() !=0)
			return (Dimension) list.get(0);
		else return null;
	}

	@Override
	public Dimension getLastObject() throws Exception {
		Query query = getSession().createQuery("from Dimension as o where o.id = (select max(id) from Dimension)");
		List<Dimension> list = query.list();
		if(list.size() !=0)
			return (Dimension) list.get(0);
		else return null;
	}

	@Override
	public Dimension findByNamePrecise(String name) throws Exception {
		List<Dimension> list = findByName(name);
		if(list.size() > 0)
			return list.get(0);
		return null;
	}
}
